home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTLAST.C < prev    next >
Text File  |  1990-06-20  |  2KB  |  92 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btlast.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8.  
  9. /* local headers */
  10. #include "btree_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      btlast - last btree key
  15.  
  16. SYNOPSIS
  17.      #include <btree.h>
  18.  
  19.      int btlast(btp)
  20.      btree_t *btp;
  21.  
  22. DESCRIPTION
  23.      The btlast function positions the cursor of btree btp on the last
  24.      key in that btree.
  25.  
  26.      btlast will fail if one or more of the following is true:
  27.  
  28.      [EINVAL]       btp is not a valid btree pointer.
  29.      [BTELOCK]      btp is not locked.
  30.      [BTENKEY]      btp is empty.
  31.      [BTENOPEN]     btp is not open.
  32.  
  33. SEE ALSO
  34.      btfirst, btkeycnt, btnext, btprev.
  35.  
  36. DIAGNOSTICS
  37.      Upon successful completion, a value of 0 is returned.  Otherwise,
  38.      a value of -1 is returned, and errno set to indicate the error.
  39.  
  40. ------------------------------------------------------------------------------*/
  41. int btlast(btp)
  42. btree_t *btp;
  43. {
  44.     int terrno = 0;        /* tmp errno */
  45.  
  46.     /* validate arguments */
  47.     if (!bt_valid(btp)) {
  48.         errno = EINVAL;
  49.         return -1;
  50.     }
  51.  
  52.     /* check if not open */
  53.     if (!(btp->flags & BTOPEN)) {
  54.         errno = BTENOPEN;
  55.         return -1;
  56.     }
  57.  
  58.     /* check locks */
  59.     if (!(btp->flags & BTLOCKS)) {
  60.         errno = BTELOCK;
  61.         return -1;
  62.     }
  63.  
  64.     /* set cursor to last key */
  65.     btp->cbtpos.node = btp->bthdr.last;
  66.  
  67.     /* check if tree is empty */
  68.     if (btp->cbtpos.node == NIL) {
  69.         btp->cbtpos.key = 0;
  70.         bt_ndinit(btp, btp->cbtnp);
  71.         errno = BTENKEY;
  72.         return -1;
  73.     }
  74.  
  75.     /* read current node */
  76.     if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  77.         BTEPRINT;
  78.         terrno = errno;
  79.         btp->cbtpos.node = NIL;
  80.         btp->cbtpos.key = 0;
  81.         bt_ndinit(btp, btp->cbtnp);
  82.         errno = terrno;
  83.         return -1;
  84.     }
  85.  
  86.     /* set cursor to last key in node */
  87.     btp->cbtpos.key = btp->cbtnp->n;
  88.  
  89.     errno = 0;
  90.     return 0;
  91. }
  92.